<template>
  <div class="bottom-nav" id="global-bottom-nav">
    <router-link to="/" class="bottom-nav-item" :class="{ active: currentTab === 'home' }" data-tab="home" @click="scrollToTop">
      <img class="bottom-nav-icon" :src="currentTab === 'home' ? require('@/assets/images/bottom_nav/ic_tabbar_home_sel.png') : require('@/assets/images/bottom_nav/ic_tabbar_home_nor.png')" alt="闲鱼">
      <span class="bottom-nav-text">闲鱼</span>
    </router-link>
    <router-link to="/local" class="bottom-nav-item" :class="{ active: currentTab === 'city' }" data-tab="city" @click="scrollToTop">
      <img class="bottom-nav-icon" :src="currentTab === 'city' ? require('@/assets/images/bottom_nav/ic_tabbar_city_sel.png') : require('@/assets/images/bottom_nav/ic_tabbar_city_nor.png')" alt="济南">
      <span class="bottom-nav-text">济南</span>
    </router-link>
    <a href="#" class="bottom-nav-sell-btn" data-tab="sell">
      <img class="bottom-nav-icon-one" :src="require('@/assets/images/bottom_nav/no_privacy_login_icon.webp')" alt="发布">
    </a>
    <router-link to="/message" class="bottom-nav-item" :class="{ active: currentTab === 'message' }" data-tab="message" @click="scrollToTop">
      <img class="bottom-nav-icon" :src="currentTab === 'message' ? require('@/assets/images/bottom_nav/ic_tabbar_message_sel.png') : require('@/assets/images/bottom_nav/ic_tabbar_message_nor.png')" alt="消息">
      <span class="bottom-nav-text">消息</span>
    </router-link>
    <router-link to="/center" class="bottom-nav-item" :class="{ active: currentTab === 'my' }" data-tab="my" @click="scrollToTop">
      <img class="bottom-nav-icon" :src="currentTab === 'my' ? require('@/assets/images/bottom_nav/ic_tabbar_my_sel.png') : require('@/assets/images/bottom_nav/ic_tabbar_my_nor.png')" alt="我的">
      <span class="bottom-nav-text">我的</span>
    </router-link>
  </div>
</template>

<script>
export default {
  name: 'BottomNav',
  computed: {
    currentTab() {
      const path = this.$route.path;
      const pathMap = {
        '/': 'home',
        '/local': 'city',
        '/message': 'message',
        '/center': 'my'
      };
      return pathMap[path] || 'home';
    }
  },
  methods: {
    scrollToTop() {
      // 延迟执行，确保路由切换完成后再滚动
      this.$nextTick(() => {
        setTimeout(() => {
          // 使用多种方法确保滚动到顶部
          window.scrollTo({
            top: 0,
            left: 0,
            behavior: 'smooth'
          });
          
          // 兼容性处理
          document.documentElement.scrollTop = 0;
          document.body.scrollTop = 0;
          
          // 处理页面级别的滚动容器
          const pageContainers = document.querySelectorAll('.index-page, .center-page, .local-page, .message-page');
          pageContainers.forEach(container => {
            if (container && container.scrollTop !== undefined) {
              container.scrollTop = 0;
            }
          });
          
          // 处理LocalPage的特殊滚动容器
          const multsScrollInner = document.querySelector('.mults-scroll-inner');
          if (multsScrollInner) {
            multsScrollInner.scrollTop = 0;
          }
          
          // 处理所有可能的滚动容器
          const allScrollContainers = document.querySelectorAll('[style*="overflow"], .swipeable-item, .content-area');
          allScrollContainers.forEach(container => {
            if (container && container.scrollTop !== undefined) {
              container.scrollTop = 0;
            }
          });
          
          // 强制刷新滚动位置
          window.scrollTo(0, 0);
          
          // 再次确保滚动到顶部
          setTimeout(() => {
            window.scrollTo(0, 0);
            document.documentElement.scrollTop = 0;
            document.body.scrollTop = 0;
          }, 50);
        }, 100);
      });
    }
  }
}
</script>

<style scoped>
.bottom-nav {
  position: fixed !important;
  bottom: 0 !important;
  left: 0 !important;
  right: 0 !important;
  background-color: #fff;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 0px 0;
  z-index: 1000 !important;
  height: 48px !important;
  transform: translateZ(0) !important;
  -webkit-transform: translateZ(0) !important;
}

.bottom-nav::before {
  content: '';
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 40px;
  background: #fff;
  border-radius: 40px 40px 0 0;
  z-index: -1;
}

.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: #666;
  flex: 1;
  position: relative;
  height: 40px;
  padding: 4px 0;
  box-sizing: border-box;
  margin: 0;
  border: 0;
  outline: none;
}

.bottom-nav-item.active {
  color: #000;
  height: 40px;
  padding: 4px 0;
  box-sizing: border-box;
}

.bottom-nav-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 2px;
  display: block;
}

.bottom-nav-icon-one {
  width: 60px;
  height: 60px;
}

.bottom-nav-text {
  font-size: 10px;
  text-align: center;
  line-height: 1;
  margin: 0;
  padding: 0;
  display: block;
}

.bottom-nav-badge {
  position: absolute;
  top: 0;
  right: 20px;
  background-color: #ff4400;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
}

.bottom-nav-sell-btn {
  background-color: #ff6900;
  color: white;
  border-radius: 50%;
  font-size: 10px;
  font-weight: bold;
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: -15px 8px 0 8px;
  position: relative;
  z-index: 1001;
  padding: 0;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  width: 60px;
  height: 60px;
}
</style>